/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.trandio.manager;

import com.google.gson.Gson;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.trandio.dao.FacturaDAO;
import org.trandio.dao.FacturaInterfase;
import org.trandio.model.PlanoACVO;
import org.trandio.model.PlanoAPVO;
import org.trandio.model.ServicioVO;

/**
 *
 * @author root
 */
public class GestionFacturaServlet extends HttpServlet {

    private FacturaInterfase facturaInterfase;
    private ServicioVO servicioVO;
    private PlanoACVO planoACVO;
    private PlanoAPVO planoAPVO;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        facturaInterfase = new FacturaDAO();

        int tipo = Integer.parseInt(request.getParameter("tipo"));

        switch (tipo) {

            case 0:
                
                int idfactura = Integer.parseInt(request.getParameter("idfactura"));

                try {
                    servicioVO = facturaInterfase.getServicioFactura(idfactura);
                } catch (SQLException ex) {
                    Logger.getLogger(GestionFacturaServlet.class.getName()).log(Level.SEVERE, null, ex);
                } catch (NamingException ex) {
                    Logger.getLogger(GestionFacturaServlet.class.getName()).log(Level.SEVERE, null, ex);
                }

                Gson gson = new Gson();
                out.println(gson.toJson(servicioVO));
                break;


            case 1:

                int iddetalleadmisionac = Integer.parseInt(request.getParameter("iddetalleadmisionac"));
                try {
                    planoACVO = facturaInterfase.getPlanoAC(iddetalleadmisionac);
                } catch (SQLException ex) {
                    Logger.getLogger(GestionFacturaServlet.class.getName()).log(Level.SEVERE, null, ex);
                } catch (NamingException ex) {
                    Logger.getLogger(GestionFacturaServlet.class.getName()).log(Level.SEVERE, null, ex);
                }

                Gson jsPlanoAc = new Gson();
                out.println(jsPlanoAc.toJson(planoACVO));
                break;

            case 2:

                int iddetalleap = Integer.parseInt(request.getParameter("iddetalle"));
                try {
                    planoAPVO = facturaInterfase.getPlanoAP(iddetalleap);
                } catch (SQLException ex) {
                    Logger.getLogger(GestionFacturaServlet.class.getName()).log(Level.SEVERE, null, ex);
                } catch (NamingException ex) {
                    Logger.getLogger(GestionFacturaServlet.class.getName()).log(Level.SEVERE, null, ex);
                }

                Gson jsPlanoAp = new Gson();
                out.println(jsPlanoAp.toJson(planoAPVO));
                break;

        }



    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
